package com.fowo.api.syncAmazon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fowo.api.syncAmazon.entity.ApiRequestLog;
import com.fowo.api.syncAmazon.service.ApiRequestLogService;
import com.fowo.api.syncAmazon.mapper.ApiRequestLogMapper;
import com.fowo.api.syncAmazon.util.AmzDateUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
* @author Administrator
* @description 针对表【amazon_data_sync_log】的数据库操作Service实现
* @createDate 2024-01-31 09:04:24
*/
@Service
public class ApiRequestLogServiceImpl extends ServiceImpl<ApiRequestLogMapper, ApiRequestLog>
    implements ApiRequestLogService {
    @Override
    public Map<String, Date> computingDates(Map<String, Object> args, String apiCode) throws Exception {
        Date startTime = null;
        Date endTime = null;
        if(args != null){
            if(args.get(START_TIME) != null && args.get(END_TIME) != null){
                startTime = AmzDateUtils.strToDate(args.get(START_TIME).toString());
                endTime = AmzDateUtils.strToDate(args.get(END_TIME).toString());
                return Map.of(START_TIME,startTime, END_TIME,endTime);
            }else{
                throw new Exception("任务调度参数不正确");
            }
        }

        //获取最新同步日志
        ApiRequestLog upToDateLog = this.getUpToDateLog(apiCode);
        if(upToDateLog == null){
            // 如果没有同步记录，则从三天前开始同步
            startTime = AmzDateUtils.getZeroOfDay(LocalDateTime.now().plusDays(-3));
            endTime = AmzDateUtils.localDateTimeToDate(LocalDateTime.now());
            return Map.of(START_TIME,startTime, END_TIME,endTime);
        }

        //设置开始日期和时间
        LocalDateTime startTimeTmp = AmzDateUtils.getLocalTime(upToDateLog.getEndTime());
        startTime = AmzDateUtils.getZeroOfDay(startTimeTmp);
        if(!startTimeTmp.toLocalDate().isBefore(LocalDate.now())){
            startTime = AmzDateUtils.getZeroOfDay(new Date());
        }

        //设置结束日期和时间
        LocalDateTime localTime = AmzDateUtils.getLocalTime(startTime).plusDays(3);
        endTime = AmzDateUtils.getDate(localTime);
        if(!localTime.toLocalDate().isBefore(LocalDate.now())){
            endTime = AmzDateUtils.localDateTimeToDate(LocalDateTime.now());
        }

        return Map.of(START_TIME,startTime, END_TIME,endTime);
    }

    @Override
    public ApiRequestLog getUpToDateLog(String amazonApiCode) {
        /**
         * 1.api接口编码
         * 2.状态为成功
         * 3.按结束时间降序
         * 4.取最新一条记录
         */
        LambdaQueryWrapper<ApiRequestLog> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ApiRequestLog::getApiCode, amazonApiCode);
        wrapper.eq(ApiRequestLog::getStatus, ApiRequestLog.SUCCESS);
        wrapper.orderByDesc(ApiRequestLog::getEndTime);
        List<ApiRequestLog> apiRequestLogs = this.getBaseMapper().selectList(wrapper);
        if(apiRequestLogs == null || apiRequestLogs.size() == 0){
            return null;
        }
        return apiRequestLogs.get(0);

    }
}




